<?php
require_once('./define/define_table.php');
require_once("./class/Adherent.php");
class Responsable extends Adherent{

    private $id_role;
    private $id_association;

    public function Responsable(){

    }

    public function getIdRole(){
        return $this->id_role;
    }

    public function setIdRole($id ){
        $this->id_role = $id;
    }

    public function getIdAssociation(){
        return $this->id_association;
    }

    public function setIdAssociation($id){
        $this->id_association = $id;
    }

    public function getAllResponsable($id_association = null){
        global $db;
        try{
            if($id_association == null){
                $sql = "SELECT *  FROM ".RESPONSABLE_TABLE."
        					JOIN ".INDIVIDU_TABLE." USING(id_individu)
                			JOIN ".ADHERENT_TABLE."	USING(id_individu)";
                $sth = $db->prepare($sql);
            }
            else{
                $sql = "SELECT * FROM ".RESPONSABLE_TABLE."
        						JOIN ".INDIVIDU_TABLE." USING(id_individu)
        						JOIN ".ADHERENT_TABLE." USING(id_individu)
        						WHERE id_association = :id_association";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
            }
            PrintQuery::queryPrint($sth);
            $sth->execute();
            return $sth->fetchAll(PDO::FETCH_CLASS, "Responsable");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }

    public function getResponsable($id_association, $id_individu){
        global $db;
        try{
            $sql = "SELECT * FROM ".RESPONSABLE_TABLE."
                						JOIN ".INDIVIDU_TABLE." USING(id_individu)
                						JOIN ".ADHERENT_TABLE." USING(id_individu)
                						WHERE id_association = :id_association
        								AND id_individu = :id_individu";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
            $sth->bindValue(":id_individu", $id_individu, PDO::PARAM_INT);
            $sth->execute();
            return $sth->fetchObject("Responsable");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }

    public function generateSelectForNews($id_association = null,$id = null){
        $array_respo = self::getAllResponsable($id_association);
        $html = '<select name="id_individu">';
        foreach($array_respo as $a){
            $selected = ($a->getIdIndividu() == $id) ?  'selected="selected"' : '';
            $html .= '<option value="'.$a->getIdIndividu().'" '.$selected.'>'.$a->getNom().' '. $a->getPrenom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }
    
    public function generateSelect($id = null){
        $array_respo = self::getAllResponsable();
        $html = '<select name="id_individu">';
        foreach($array_respo as $a){
            $selected = ($a->getIdIndividu() == $id) ?  'selected="selected"' : '';
            $html .= '<option value="'.$a->getIdIndividu().'" '.$selected.'>'.$a->getNom().' '. $a->getPrenom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }

    public function addOrModify(){
        global $db;
        try{
            $sql = "SELECT COUNT(*) FROM ".RESPONSABLE_TABLE." WHERE id_individu = :id_individu AND id_association = :id_association AND id_role = :id_role";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
            $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
            $sth->bindValue(":id_role", $this->id_role, PDO::PARAM_INT);
            $sth->execute();
            $nb = $sth->fetch();
            if($nb[0] == 0){
                // Ajout
                $sql = "INSERT INTO ".RESPONSABLE_TABLE." 
        	            		VALUES(	:id_individu, :id_role, :id_association)";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
                $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
                $sth->bindValue(":id_role", $this->id_role, PDO::PARAM_INT);
            }
            else{ // Update
                $sql = "UPDATE ".RESPONSABLE_TABLE." 
        	            	    SET id_role = :id_role
        	            	    WHERE id_individu = :id_individu
        	            	    AND
        	            	    id_association =  :id_association";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
                $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
                $sth->bindValue(":id_role", $this->id_role, PDO::PARAM_INT);
            }
            PrintQuery::queryPrint($sth);
            $sth->execute();
        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }

    public function delete(){
        global $db;
        try{
            $sql = "DELETE FROM ".RESPONSABLE_TABLE." WHERE id_individu = :id_individu AND id_association = :id_association";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
            $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
            $sth->execute();
            PrintQuery::queryPrint($sth);
        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }

    public static function getAddForm($id_association = null){
        $html = ADD_RESPONSABLE_FORM .
        				'<form  action="?module=responsable&action=doAdd" method="post">' .
        				'	<table>' .
        				'		<tr>' .
        				'			<td>'._ADHERENT_.'</td>' .
        				'			<td>'.Adherent::generateSelectForAssociation($id_association).'</td>' .
        				'		</tr>' .
        				'		<tr>' .
        				'			<td>'._ASSOCIATION_.'</td>' .
        				'			<td>'.Association::generateSelect($id_association).'</td>' .
        				'		</tr>' .
        				'		<tr>' .
        				'			<td>'._ROLE_.'</td>' .
        				'			<td>'.Role::generateSelect().'</td>' .
        				'		</tr>' .
        				'		<tr>' .
        				'			<td colspan="2"><input type="submit" />' .
        				'		</tr>' .
        				'	</table>' .
        				'</form>';
        return $html;
    }

    public function getModifyForm($id_association = null){
        $html = MODIFY_RESPONSABLE_FORM .
            				'<form  action="?module=responsable&action=doModify" method="post">' .
            				'	<table>' .
            				'		<tr>' .
            				'			<td>'._ADHERENT_.'</td>' .
            				'			<td>'.Adherent::generateSelectForAssociation($this->id_association, $this->id_individu).'</td>' .
            				'		</tr>' .
            				'		<tr>' .
            				'			<td>'._ASSOCIATION_.'</td>' .
            				'			<td>'.Association::generateSelect($this->id_association).'</td>' .
            				'		</tr>' .
            				'		<tr>' .
            				'			<td>'._ROLE_.'</td>' .
            				'			<td>'.Role::generateSelect($this->id_role).'</td>' .
            				'		</tr>' .
            				'		<tr>' .
            				'			<td colspan="2"><input type="submit" />' .
            				'		</tr>' .
            				'	</table>' .
            				'</form>';
        return $html;
    }
}
?>
